import json
import requests

from setting import openDbBase

class apiList:
    dbproconfig = openDbBase()
    compno = dbproconfig['COMPNO']
    # compno = "NYJS"
    # 广播标签名称
    # broadcastName = "data"
    broadcastName = dbproconfig['BROADCASTNAME']
    broadcastactionName = dbproconfig['BROADCASTACTIONNAME']
    apiAdd = dbproconfig['APIADD']
    # apiAdd = "http://NIUBI/ERPAPI/"

    # "d5e2a970-1e41-42ca-a639-118931f14083"
    # 用户登录pyinstaller -F serve.py
    @classmethod
    def login(cls,usr,pwd):
        url = cls.apiAdd + "auth/login"
        jsonData = {
            "COMPNO":cls.compno,
            "USR":usr,
            "PWD":pwd
        }
        header = {
            "Content-Type":"application/json"
        }
        r = requests.post(url,json=jsonData,headers=header)
        if r.status_code != 200:
            print(r.json())
            print(r.status_code)
            return None, '登录接口请求失败'
        resData = r.json()
        if resData['code']!=0:
            return None,str(resData['code']) + resData['message']
        data = {"token":resData['data']['TOKEN'],"compno":cls.compno,"broadcastName":cls.broadcastName,"broadcastactionName":cls.broadcastactionName}
        return data,None
    # 新增进货验收单
    @classmethod
    def tyAdd(cls,data,token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPTY/TRANDATA"
        data["PGM"] = 'DRPTY'
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "Authorization":token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '登录接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        # print(resData)
        data = resData['data']['MF_TY'][0]['TY_NO']
        return data, None
    # 删除进货验收单
    @classmethod
    def tyDelete(cls, ty_no, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPTY/DELETEDATA"
        data = {}

        data["PGM"] = 'DRPTY'
        data["TY_ID"] = "TY"
        data["TY_NO"] = ty_no
        jsonData = data

        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '登录接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        return None, None

    # 新增制成品验收单
    @classmethod
    def tpAdd(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPTY/TRANDATA"
        data["PGM"] = 'MRPBN'
        jsonData = data
        # pprint(jsonData)
        print(999)
        print(json.dumps(jsonData, ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '登录接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        print(resData)
        data = resData['data']['MF_TY'][0]['TY_NO']
        return data, None
    # 删除制成品验收
    @classmethod
    def tpDelete(cls, ty_no, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPTY/DELETEDATA"
        data = {}

        data["PGM"] = 'MRPBN'
        data["TY_ID"] = "TP"
        data["TY_NO"] = ty_no
        jsonData = data

        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除制成品验收接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        return None, None

    # 新增托工验收单
    @classmethod
    def toAdd(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPTY/TRANDATA"
        data["PGM"] = 'MRPTO'
        jsonData = data
        # pprint(jsonData)
        print(999)
        print(json.dumps(jsonData, ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '新增托工验收单接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        print(resData)
        data = resData['data']['MF_TY'][0]['TY_NO']
        return data, None

    # 删除托工验收
    @classmethod
    def toDelete(cls, ty_no, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPTY/DELETEDATA"
        data = {}

        data["PGM"] = 'MRPTO'
        data["TY_ID"] = "TO"
        data["TY_NO"] = ty_no
        jsonData = data

        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除托工验收接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        return None, None
            # apiList.login('ADMIN','')


# import socket
# # 获取本地主机的真实IP
# def get_ip():
#     try:
#         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#         s.connect(('8.8.8.8', 80))
#         ip = s.getsockname()[0]
#     finally:
#         s.close()
#     print(ip)
#     return ip
# get_ip()


    # 免检转托工缴回
    @classmethod
    def tbmjAdd(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPBF/TRANDATA"
        data["PGM"] = 'MRPBF'
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '免检转托工缴回接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        # print(resData)
        data = resData['data']['MF_TB'][0]['TB_NO']
        return data, None

    # 托工验收转托工缴库单
    @classmethod
    def tbAdd(cls, data, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPBF/TRANDATA"
        data["PGM"] = 'MRPBF'
        jsonData = data
        # pprint(jsonData)
        # print(json.dumps(jsonData,ensure_ascii=False))
        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '托工验收转托工缴库单接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        # print(resData)
        data = resData['data']['MF_TB'][0]['TB_NO']
        return data, None

    # 删除托工缴库单
    @classmethod
    def tbDelete(cls, tb_no, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPBF/DELETEDATA"
        data = {}

        data["PGM"] = 'MRPBF'
        data["TB_NO"] = tb_no
        jsonData = data

        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除托工缴库单接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        return None, None

    # 生产生产日报单
    @classmethod
    def wradd(cls, data, token):
        """
        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPEG/ADDDATA"

        data["PGM"] = 'MRPEG'
        data["SKIP_ACTION"] = 'T'
        jsonData = data

        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '生产日报单接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        data = resData['data']['MF_WR'][0]['WR_NO']
        return data, None

    # 删除生产日报
    @classmethod
    def wrDelete(cls, no, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPEG/DELETEDATA"
        data = {}

        data["PGM"] = 'MRPEG'
        data["WR_NO"] = no
        jsonData = data

        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除生产日报接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        return None, None

    # 制令转成品缴库
    @classmethod
    def mmoMoAdd(cls,data, token):
        """
        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPAFC/TRANDATA"

        data["PGM"] = 'MRPAFC'
        data["SKIP_ACTION"] = 'T'
        jsonData = data

        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '成品缴库单接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        data = resData['data']['MF_MM0'][0]['MM_NO']
        return data, None

    # 删除成品缴库单
    @classmethod
    def mmoMoDelete(cls, no, token):
        """

        :param data: 表身参数
        :param token: 请求头token
        :return:
        """
        url = cls.apiAdd + "MRPAFC/DELETEDATA"
        data = {}

        data["PGM"] = 'MRPAFC'
        data["MM_NO"] = no
        data["MM_ID"] = "MM"
        jsonData = data

        header = {
            "Content-Type": "application/json",
            "Authorization": token
        }
        r = requests.post(url, json=jsonData, headers=header)
        if r.status_code != 200:
            return None, '删除成品缴库接口请求失败'
        resData = r.json()
        if resData['code'] != 0:
            return None, str(resData['code']) + resData['message']
        return None, None